package com.esg.esgproject.mybatisplus;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.util.Scanner;
/**
 * @author Z100
 * @create 2021-07-27 10:05
 * @desc 自动生成 mybatis-plus 的相关代码
 **/


/**
 *
 */
public class GeneratorCodeConfig {
    private static final String url = "jdbc:mysql://8.136.212.218:3306/" +
            "esgdb?" +
            "serverTimezone=Asia/Shanghai" +
            "&useUnicode=true" +
            "&characterEncoding=utf-8" +
            "&zeroDateTimeBehavior=convertToNull" +
            "&useSSL=false" +
            "&allowPublicKeyRetrieval=true";
    private static final String username = "root";
    private static final String password = "123456";

    public static String scanner(String tip) {
        Scanner scanner = new Scanner(System.in);
        StringBuilder help = new StringBuilder();
        help.append("请输入" + tip + "：");
        System.out.println(help.toString());
        if (scanner.hasNext()) {
            String ipt = scanner.next();
            if (StringUtils.isNotEmpty(ipt)) {
                return ipt;
            }
        }
        throw new MybatisPlusException("请输入正确的" + tip + "！");
    }

    public static void main(String[] args) {
        // 代码生成器
        AutoGenerator mpg = new AutoGenerator();

        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        //C:\Users\hp\Desktop\esg\esgproject\src
        String path="C:\\Users\\hp\\Desktop\\esg\\esgproject";
        gc.setOutputDir(path + "/src/main/java");
        gc.setAuthor("z100");
        gc.setServiceName("%sService");	//去掉Service接口的首字母I
        gc.setFileOverride(true); //重新生成时文件是否覆盖
        gc.setIdType(IdType.ID_WORKER); //主键策略
        gc.setOpen(false);//生成后是否打开资源管理器
        gc.setSwagger2(false);//实体属性 Swagger2 注解 开启Swagger2模式

        mpg.setGlobalConfig(gc);

        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl(url);
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUsername(username);
        dsc.setPassword(password);
        mpg.setDataSource(dsc);

        // 包配置
        PackageConfig pc = new PackageConfig();
//        pc.setModuleName(scanner("模块名"));
        pc.setParent("com.esg.esgproject.mp");

        pc.setEntity("entity.auto");
        pc.setMapper("mapper.auto");
        pc.setService("service");
        pc.setServiceImpl("service.impl");
        mpg.setPackageInfo(pc);

        // 配置模板
        TemplateConfig templateConfig = new TemplateConfig();

        templateConfig.setXml(null);
        mpg.setTemplate(templateConfig);

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setSuperEntityClass("com.baomidou.mybatisplus.extension.activerecord.Model");
        strategy.setEntityLombokModel(true);
        strategy.setRestControllerStyle(true);

        strategy.setEntityLombokModel(true);

        strategy.setInclude(scanner("表名，多个英文逗号分割").split(","));
        strategy.setControllerMappingHyphenStyle(true);
        strategy.setTablePrefix(pc.getModuleName() + "_");
        mpg.setStrategy(strategy);
        mpg.setTemplateEngine(new FreemarkerTemplateEngine());
        mpg.execute();
    }
}
